package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.BuildingMapper;
import com.kgc.community.mapper.RoomsMapper;
import com.kgc.community.mapper.UserMapper;
import com.kgc.community.pojo.*;
import com.kgc.community.service.RoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoomsServiceImpl implements RoomsService {

    @Autowired
    RoomsMapper roomsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    BuildingMapper buildingMapper;

//    @Override
//    public PageInfo<Rooms> selectAllRoomsVO(String id, Integer pageNum, Integer pageSize) {
//        RoomsExample roomsExample=new RoomsExample();
//        roomsExample.createCriteria().andIdEqualTo(id);
//
//        PageHelper.startPage(pageNum,pageSize);
//        PageHelper.orderBy("id desc");
//
//
//        List<Rooms> rooms = roomsMapper.selectByExample(roomsExample);
//
//        if(rooms!=null&&rooms.size()>0){
//            System.out.println(rooms.get(0).toString());
//        }
//
//        PageInfo<Rooms> pageInfo=new PageInfo<>(rooms);
//
//        System.out.println(pageInfo.getList().get(0).getUser().getName());
//
//        return pageInfo;
//    }
    @Override
    public PageInfo<Rooms> selectAllRoomsVO(String id, Integer pageNum, Integer pageSize) {
      List<Rooms> rooms=new ArrayList<>();
      List<User> users=new ArrayList<>();
      List<Building> buildings=new ArrayList<>();
      if (!id.equals("%%")){
          UserExample example=new UserExample();
          example.createCriteria().andNameLike(id);
          users = userMapper.selectByExample(example);

          RoomsExample example1=new RoomsExample();
          example1.createCriteria().andIdLike(id);
          rooms = roomsMapper.selectByExample(example1);

      }

        //设置当前页和页容量，顺序不能改变
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        RoomsExample example2=new RoomsExample();
        if(!id.equals("%%")){
            if(users!=null &users.size()>0){
                List<Integer> ids = new ArrayList<>();
                for(int i=0;i<users.size();i++){
                    ids.add(users.get(i).getId());
                }
                example2.createCriteria().andUserIdIn(ids);

            }
            if(users==null || users.size()==0){
                users=new ArrayList<>();

                User user = new User();
                user.setId(-1);
                example2.createCriteria().andUserIdEqualTo(user.getId());

            }
            RoomsExample.Criteria criteria2 = example2.createCriteria();
            if(rooms!=null & rooms.size()>0){
                List<String> ids = new ArrayList<>();
                for(int j=0;j<rooms.size();j++){
                    ids.add(rooms.get(j).getId());
                }
                criteria2.andIdIn(ids);
            }
            if(rooms==null||rooms.size()==0){
                rooms=new ArrayList<>();
                Rooms  room = new Rooms();
                room.setId("-1");
                criteria2.andIdEqualTo(room.getId());

            }
            example2.or(criteria2);
        }
        List<Rooms> rooms1 = roomsMapper.selectByExample(example2);
        PageInfo<Rooms> pageInfo=new PageInfo<>(rooms1);
        return pageInfo;
    }

    @Override
    public List<Rooms> selectRoomsId() {
        return roomsMapper.selectByExample(null);
    }



    @Override
    public Integer add(Rooms rooms,User user,Building building) {
        Integer count1 = roomsMapper.insertSelective(rooms);
        Integer count2 = userMapper.insertSelective(user);
        Integer count3 = buildingMapper.insertSelective(building);
        Integer count=0;
        if(count1>0&count2>0&count3>0){
            count=1;
        }
        return count;
    }

    @Override
    public Rooms selectRooms(String id) {
        RoomsExample rooms=new RoomsExample();
        rooms.createCriteria().andIdEqualTo(id);
        List<Rooms> rooms1 = roomsMapper.selectByExample(rooms);
        if(rooms1!=null) {
            for (Rooms rooms2 : rooms1) {
                return rooms2;
            }
        }
        return null;
    }

    @Override
    public int modifyRooms(Rooms rooms, User user, Building building) {
        rooms.setUser(user);
        rooms.setBuilding(building);
        Integer count1 = roomsMapper.updateByPrimaryKeySelective(rooms);
        Integer count2 = userMapper.updateByPrimaryKeySelective(user);
        Integer count3 = buildingMapper.updateByPrimaryKeySelective(building);
        Integer count=0;
        if(count1>0&count2>0&count3>0){
            count=1;
        }
        return count;
    }

    @Override
    public int deleteRooms(String id) {
        return roomsMapper.deleteByPrimaryKey(id);
    }

}
